#! /bin/bash
#系统更新
 apt update -y
  echo -e "系统更新成功"
   sleep 3 
    #  证书机构
     apt install easy-rsa openssl openvpn -y  
      echo -e "证书以及openvpn安装成功"
       sleep 3 
        #生成证书环境变量
	 mkdir /opt/easy-rsa
	  echo -e "创建目录成功"
	   sleep 3
	    cp -a /usr/share/easy-rsa/* /opt/easy-rsa/
	     sleep 3
	      #配置个人信息
	       cat >> /opt/easy-rsa/vars <<EOF
export KEY_COUNTRY="CN"   
export KEY_PROVINCE="SD"   
export KEY_CITY="QingDao"   
export KEY_ORG="xiaolu"     
export KEY_EMAIL="349281613@qq.com"    
EOF
sleep 2
echo -e "配置文件生成成功"
#创建pki 目录
cd /opt/easy-rsa/
./easyrsa init-pki
echo -e "创建pki目录成功"
sleep 3
echo -e "创建根证书 "
./easyrsa --batch build-ca nopass
echo -e "创建根证书成功"
sleep 3
EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-server-full server nopass
echo -e "服务端签名成功"
sleep 3
echo '-----BEGIN DH PARAMETERS-----
MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg==
-----END DH PARAMETERS-----' >> /etc/openvpn/server/dh.pem  
echo -e "diffie-hellman算法成功"
sleep 3 
EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full  client  nopass
echo -e "客户端证书签名成功"
sleep 3 
#生成tls-crypt证书
openvpn --genkey --secret /etc/openvpn/server/tc.key
echo -e "生成tls-crypt证书成功"
sleep 3 
#创建日志目录
mkdir /etc/openvpn/logs -p
#添加证书
\cp /opt/easy-rsa/pki/ca.crt /etc/openvpn/server/
\cp /opt/easy-rsa/pki/issued/server.crt /etc/openvpn/server/
\cp /opt/easy-rsa/pki/private/server.key /etc/openvpn/server/
\cp /opt/easy-rsa/pki/ca.crt /etc/openvpn/client/
\cp /opt/easy-rsa/pki/private/client.key /etc/openvpn/client/
\cp /opt/easy-rsa/pki/issued/client.crt /etc/openvpn/client/
\cp /etc/openvpn/server/tc.key /etc/openvpn/client/
#配置server 文件
cat >/etc/openvpn/server.conf <<EOF
port 11941
proto udp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server.crt
key /etc/openvpn/server/server.key
dh /etc/openvpn/server/dh.pem
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 114.114.114.114"
ifconfig-pool-persist /etc/openvpn/logs/ipp.txt
keepalive 10 120
max-clients 100
status /etc/openvpn/logs/openvpn-status.log
verb 3
client-to-client
log /etc/openvpn/logs/openvpn.log
persist-key
persist-tun
duplicate-cn
auth SHA512
tls-crypt /etc/openvpn/server/tc.key
topology subnet
EOF
#系统内核转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo echo -e "ip内核转发成功共享上网成功"
#开机并且启动
systemctl enable openvpn.service
systemctl start  openvpn.service
mkdir /root/xiaolu -p
cp -a /etc/openvpn/client/* /root/xiaolu
sleep 3
echo -e "
client
dev tun
proto udp
remote 10.0.0.61 11941
resolv-retry infinite
nobind
ca ca.crt
cert client.crt
key client.key
auth SHA512
tls-crypt tc.key
topology subnet
verb 3
persist-key
">>/root/xiaolu/xiaolu.ovpn
sleep 3
tar -zcvf /root/xiaolu.tar.gz /root/xiaolu
apt install rinetd -y
echo -e "rinetd 安装成功"
rm -rf /etc/rinetd.conf
touch /etc/rinetd.conf
echo -e "0.0.0.0 31400 10.8.0.6 31400
         0.0.0.0 31401 10.8.0.6 31401
	          0.0.0.0 31402 10.8.0.6 31402
		           0.0.0.0 31403 10.8.0.6 31403
			            0.0.0.0 31404 10.8.0.6 31404
				             0.0.0.0 31405 10.8.0.6 31405
					              0.0.0.0 31406 10.8.0.6 31406
						               0.0.0.0 31407 10.8.0.6 31407
							                0.0.0.0 31408 10.8.0.6 31408
									         0.0.0.0 31409 10.8.0.6 31409
										          0.0.0.0 31410 10.8.0.6 31410" >> /etc/rinetd.conf

											  rinetd -c /etc/rinetd.conf
echo -e "架设成功,请验收我是笨蛋刘晓陆"

